﻿using DongFangGuoKai.WMSPlatform.Core.Repository;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using DongFangGuoKai.WMSPlatform.Service.Permissions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Security.Cryptography;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.Permissions
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ApiBaseController
    {
        private readonly IRepository<TRole> rolerepost;
        private readonly IPermissionService permissionService;

        public RoleController(IRepository<TRole> rolerepost,
            IPermissionService permissionService
            )
        {
            this.rolerepost = rolerepost;
            this.permissionService = permissionService;
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="rnam"></param>
        /// <param name="state"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleList(string? rnam = "", int state = 3, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Failed("分页条件有误！");
            }
            var rlist = rolerepost.GetList();
            var count = rlist.Count;
            if (!string.IsNullOrEmpty(rnam))
            {
                rlist = rlist.Where(x=>x.RoleName.Contains(rnam)).ToList();
            }
            if (state != 3)
            {
                rlist = rlist.Where(x => x.Status == state).ToList();
            }
            rlist = rlist.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (rlist != null)
            {
                return Success(rlist, count, "查询成功！");
            }
            return Failed("查询失败！");
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRole(TRole m)
        {
            var obj = rolerepost.Get(r=>r.RoleCode == m.RoleCode);
            if (obj != null)
            {
                return Failed("添加失败！已存在角色编码！");
            }
            m.CreateDate = DateTime.Now;
            if (rolerepost.Insert(m) == null)
            {
                return Failed("添加失败！");
            }
            return Success("添加成功！");
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRole(int rid)
        {
            if (rid <= 0)
            {
                return Failed("Id有误！");
            }
            if (permissionService.DelRole(rid)) {
                return Success("删除成功！");
            }
            return Failed("删除失败！");
        }
        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="ridList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRoleList(int[] ridList)
        {
            if (ridList.Length <= 0)
            {
                return Failed("Id有误！");
            }
            foreach (var rid in ridList)
            {
                bool IsOK = permissionService.DelRole(rid);
                if (!IsOK)
                {
                    return Failed("删除失败！");

                }
                else
                {
                    continue;
                }
            }
            return Success("删除成功！");
        }

        /// <summary>
        /// 根据主键获取角色
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RoleById(int rid)
        {
            if (rid <= 0)
            {
                return Failed("Id有误！");
            }
            var robj = rolerepost.Get(r=>r.Id == rid);
            if (robj != null)
            {
                return Success(robj,"查询成功！");
            }
            return Failed("查询失败！");
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdRole(TRole m)
        {
            if (m.Id <= 0)
            {
                return Failed("Id有误！");
            }
            var roleobj = rolerepost.Get(r=>r.Id != m.Id && r.RoleCode == m.RoleCode);
            if (roleobj != null)
            {
                return Failed("修改失败！已存在角色编码！");
            }
            m.ModifyDate = DateTime.Now;
            if (!rolerepost.Update(m))
            {
                return Failed("修改失败！");
            }
            return Success("修改成功！"); 
        }

        /// <summary>
        /// 权限分配列表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleMenuActionList()
        {
            var rlist = permissionService.GetRoleMenuActionList();
            if (rlist != null)
            {
                return Success(rlist,"查询成功！");
            }
            return Failed("查询失败！");
        }
    }
}
